package com.splic.web.controller.product;

import com.github.pagehelper.PageInfo;
import com.splic.common.core.controller.BaseController;
import com.splic.common.core.page.TableDataInfo;
import com.splic.common.utils.DateUtils;
import com.splic.product.domain.Channel;
import com.splic.product.domain.Product;
import com.splic.product.dto.DeviceStatisDto;
import com.splic.product.service.ChannelService;
import com.splic.product.service.DeviceStatisService;
import com.splic.product.service.ProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 用户设备Controller
 * 
 * @author singe
 * @date 2022-01-04
 */
@Controller
@RequestMapping("/product/deviceStatis")
public class DeviceStatisController extends BaseController
{
    private String prefix = "product/statistics/deviceStatis";

    @Autowired
    private DeviceStatisService deviceStatisServiceImpl;
    @Autowired
    private ProductService productServiceImpl;
    @Autowired
    private ChannelService channelServiceImpl;

    @RequiresPermissions("product:deviceStatis:view")
    @GetMapping("/toAddlist")
    public String deviceStatis(ModelMap mmap)
    {
        List<Channel> channelList = channelServiceImpl.selectChannelList(null);
        mmap.put("channelList",channelList);
        List<Product> productList = productServiceImpl.selectProductList(null);
        mmap.put("productList",productList);
        mmap.put("startCreateTime", DateUtils.addDays(new Date(),-7));
        mmap.put("endCreateTime",new Date());

        List<String> phoneBrandList = deviceStatisServiceImpl.selectAddPhoneBrandList();
        mmap.put("phoneBrandList",phoneBrandList);
        List<String> androidVersionList = deviceStatisServiceImpl.selectAddAndroidVersionList();
        mmap.put("androidVersionList",androidVersionList);
        return prefix + "/addlist";
    }

    /**
     * 查询用户设备列表
     */
    @RequiresPermissions("product:deviceStatis:list")
    @PostMapping("/addlist")
    @ResponseBody
    public TableDataInfo addlist(DeviceStatisDto deviceStatisDto)
    {
        if (deviceStatisDto.getViewChannelId() == 0
                && deviceStatisDto.getViewProductId() == 0
                && deviceStatisDto.getViewPhoneBrand() == 0
                && deviceStatisDto.getViewAndroidVersion() == 0 ){
            deviceStatisDto.setViewDate(1);
        }
        startPageNoOrderBy();
        List<DeviceStatisDto> list = deviceStatisServiceImpl.selectAddStatisPageList(deviceStatisDto);

        DeviceStatisDto dto = new DeviceStatisDto();
        dto.setReturnCount(0);
        for (DeviceStatisDto origin : list) {
            dto.setReturnCount(dto.getReturnCount() + origin.getReturnCount());
        }
        dto.setItem("总计");
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setTotal(new PageInfo(list).getTotal());
        list.add(dto);
        rspData.setRows(list);
        return rspData;
    }

    /**
     * 新增或修改保存其他服et务与卡群组绑定
     */
//    @GetMapping("/addChangePhoneBrand")
//    @ResponseBody
//    public List<String> addChangePhoneBrand(String phoneBrand)
//    {
//        List<String> androidVersionList = deviceStatisServiceImpl.selectAddAndroidVersionList(phoneBrand);
//        return androidVersionList;
//    }



    @RequiresPermissions("product:deviceStatis:view")
    @GetMapping("/toActivelist")
    public String activelist(ModelMap mmap)
    {
        List<Product> productList = productServiceImpl.selectProductList(null);
        mmap.put("productList",productList);
        mmap.put("startCreateTime", DateUtils.addDays(new Date(),-7));
        mmap.put("endCreateTime",new Date());

        List<String> phoneBrandList = deviceStatisServiceImpl.selectActivePhoneBrandList();
        mmap.put("phoneBrandList",phoneBrandList);

        return prefix + "/activelist";
    }

    /**
     * 查询用户设备列表
     */
    @RequiresPermissions("product:deviceStatis:list")
    @PostMapping("/activelist")
    @ResponseBody
    public TableDataInfo activelist(DeviceStatisDto deviceStatisDto)
    {
        if (deviceStatisDto.getViewChannelId() == 0
                && deviceStatisDto.getViewProductId() == 0
                && deviceStatisDto.getViewPhoneBrand() == 0
                && deviceStatisDto.getViewAndroidVersion() == 0 ){
            deviceStatisDto.setViewDate(1);
        }
        startPageNoOrderBy();
        List<DeviceStatisDto> list = deviceStatisServiceImpl.selectActiveStatisPageList(deviceStatisDto);

        DeviceStatisDto dto = new DeviceStatisDto();
        dto.setReturnCount(0);
        for (DeviceStatisDto origin : list) {
            dto.setReturnCount(dto.getReturnCount() + origin.getReturnCount());
        }
        dto.setItem("总计");
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setTotal(new PageInfo(list).getTotal());
        list.add(dto);
        rspData.setRows(list);
        return rspData;
    }

    /**
     * 新增或修改保存其他服et务与卡群组绑定
     */
    @GetMapping("/activeChangePhoneBrand")
    @ResponseBody
    public List<String> activeChangePhoneBrand(String phoneBrand)
    {
        List<String> androidVersionList = deviceStatisServiceImpl.selectActiveAndroidVersionList(phoneBrand);
        return androidVersionList;
    }

}
